package com.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.design.widget.TabLayout;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ddd.wandemo.R;
import com.util.DensityUtils;

import java.lang.reflect.Field;
import java.util.ArrayList;

/**
 * 修改tablayout指示线长度
 */
public class ShortIndicatorTabLayout extends TabLayout {
    private Context context;
    private static final int defaultWith = 130;
    private int mIndicatorWith;
    private boolean mSameWith;

    public ShortIndicatorTabLayout(Context context) {
        this(context,null);
    }

    public ShortIndicatorTabLayout(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public ShortIndicatorTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        if (attrs != null) {
            TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ShortIndicatorTabLayout, defStyleAttr, 0);
            mIndicatorWith = ta.getInt(R.styleable.ShortIndicatorTabLayout_set_indicator_with, defaultWith);
            mSameWith = ta.getBoolean(R.styleable.ShortIndicatorTabLayout_same_with_text, false);
            ta.recycle();
        }
        this.context = context;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int margin = DensityUtils.dp2px(context, 30);
        LinearLayout mTabStrip = (LinearLayout) this.getChildAt(0);
        try {
            Field mTabs = TabLayout.class.getDeclaredField("mTabs");
            mTabs.setAccessible(true);
            ArrayList<Tab> tabs = (ArrayList<Tab>) mTabs.get(this);
            for (int i = 0; i < mTabStrip.getChildCount(); i++) {
//                Tab tab = tabs.get(i);
//                Field mView = tab.getClass().getDeclaredField("mView");
//                mView.setAccessible(true);
//                Object tabView = mView.get(tab);
//                Field mTabView = context.getClassLoader().loadClass("android.support.design.widget.TabLayout$TabView").getDeclaredField("mTextView");
//                mTabView.setAccessible(true);
//                TextView textView = (TextView) mTabView.get(tabView);
//                float textWidth = textView.getPaint().measureText(textView.getText().toString());
                View child = mTabStrip.getChildAt(i);
                child.setPadding(0, 0, 0, 0);
//                LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) child.getLayoutParams();
                if (mSameWith) {
                    Tab tab = tabs.get(i);
                    Field mView = tab.getClass().getDeclaredField("mView");
                    mView.setAccessible(true);
                    Object tabView = mView.get(tab);
                    Field mTabView = context.getClassLoader().loadClass("android.support.design.widget.TabLayout$TabView").getDeclaredField("mTextView");
                    mTabView.setAccessible(true);
                    TextView textView = (TextView) mTabView.get(tabView);
                    mIndicatorWith = (int) textView.getPaint().measureText(textView.getText().toString());
                }
//                params.width = mIndicatorWith;
//                params.height = LinearLayout.LayoutParams.MATCH_PARENT;
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(mIndicatorWith, LinearLayout.LayoutParams.MATCH_PARENT);

                params.leftMargin = margin;
                params.rightMargin = margin;
                child.setLayoutParams(params);
                child.invalidate();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
